package com.qfedu.edu.seckill.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.qfedu.edu.seckill.constance.RabbitMQConstance;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author xiaobobo
 * @title: MySimpleJob
 * @projectName cd-java-2404-seckill-parent
 * @description: TODO
 * @date 2025/2/11  15:43
 */
@Component
@Slf4j
public class MySimpleJob implements SimpleJob {

    @Autowired
    private Channel channel;

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("定时任务执行了....");
        //在这里就可以编写业务代码了...
        //首先获取所有队列的名字
        String[] queueArray = RabbitMQConstance.getQueueArray();
        for (int i = 0; i <queueArray.length ; i++) {
            //找到这个队列的名字
            String queueName = queueArray[i];
            try {
                AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive(queueName);
                //获取到消费者的个数
                int consumerCount = declareOk.getConsumerCount();
                //获取到消息的条目数
                int messageCount = declareOk.getMessageCount();
                //然后再这里就可以和预期值进行比较了
                if(consumerCount==0){
                    //预警(短信/邮件....)
                    log.error("队列{}消费者的个数为0",queueName);
                }
                if(messageCount>3){
                    log.error("队列{}消息的个数 达到了指定的阈值",queueName);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
